﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Xiaowei.Services
{
    public interface IBrightnessService
    {
        byte Brightness
        {
            get;set;
        }
    }
    internal class BrightnessService: IBrightnessService
    {
        public BrightnessService() {
        }
        public byte Brightness
        {
            get
            {
                ManagementScope scope = new ManagementScope("root\\WMI");
                SelectQuery query = new SelectQuery("WmiMonitorBrightness");
                byte b = 0;
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
                {
                    using (ManagementObjectCollection objectCollection = searcher.Get())
                    {
                        foreach (ManagementObject mObj in objectCollection)
                        {
                            b = (byte)mObj.Properties["CurrentBrightness"].Value;
                            break;
                        }
                    }
                }
                return b;
            }
            set
            {
                ManagementScope scope = new ManagementScope("root\\WMI");
                SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
                {
                    using (ManagementObjectCollection objectCollection = searcher.Get())
                    {
                        foreach (ManagementObject mObj in objectCollection)
                        {
                            mObj.InvokeMethod("WmiSetBrightness",
                                new Object[] { UInt32.MaxValue, value });
                            break;
                        }
                    }
                }
            }
        }
    }
}
